package com.khyr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.entity.*;
import com.khyr.service.HisPatientService;
import com.khyr.service.UserService;
import com.khyr.utils.SnowflakeIdGenerator;
import com.khyr.vo.GetUserListByDeptIdQDto;
import com.khyr.vo.R;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/user")
public class UserController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;
    @Autowired
    private HisPatientService hisPatientService;
    @GetMapping("/getByName/{name}")
    public R<User> getByName(@PathVariable String name) {
        return userService.getByName(name);
    }
    @GetMapping("/getUserInfo/{token}")
    public R<User> getUserInfo(@PathVariable  String token) {
//        String token = request.getHeader("token");
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        String token = request.getHeader("token");

        System.out.println("token:"+token);
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> r = userService.getByName(phone.toString());
        return r;
    }
    @GetMapping("/getPatientInfo/{token}")
    public R<HisPatient> getPatientInfo(@PathVariable  String token) {
//        String token = request.getHeader("token");
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        String token = request.getHeader("token");

        System.out.println("token:"+token);
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<HisPatient> r = userService.getByPatientName(phone.toString());
        return r;
    }
    @GetMapping("/getUserInfo2")
    public R<User> getUserInfo2(HttpServletRequest request) {
        String token = request.getHeader("token");
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> r = userService.getByName(phone.toString());
        return r;
    }

    @GetMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        redisTemplate.delete("user::login::" + token);
        return new R<>(200,"退出成功",null);
    }

    @GetMapping("/getAllSchedulingList")
    public R<List<User>> getAllSchedulingList() {
        return userService.getAllSchedulingList();
    }
    //根据条件查询医生信息
    @PostMapping("/getSchedulingDoctorByCon")
    public R<List<User>> getSchedulingDoctorByCon(@RequestBody Map<String,Object> map) {
        return userService.getSchedulingDoctorByCon(map);
    }
    @GetMapping("/getUserListByDeptIdQ/{deptId}")
    public R<List<User>> getUserListByDeptIdQ(@PathVariable Integer deptId) {
        // 手机端的首页中根据部门id查询医生排班信息
        return userService.getUserListByDeptId(deptId);
    }
    @GetMapping("/getUserByIdQ/{id}")
    public R<GetUserListByDeptIdQDto> getUserByIdQ(@PathVariable Long id) {
        // 手机端首页根据医生id查询医生信息
        return userService.getUserByIdQ(id);
    }
    //手机端登录
    @GetMapping("/getByName2/{name}")
    public R<HisPatient> getByName2(@PathVariable String name) {
        return hisPatientService.getByName(name);

    }
    //手机端加载医生列表
    @GetMapping("/getUserListRanges")
    public R<List<User>> getUserListRanges() {
        return userService.getUserListRanges();
    }
    //手机端-我的-加载个人信息
    @GetMapping("/getPatientInfo")
    public R<HisPatient> getPatientInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<HisPatient> r = userService.getByPatientName(phone.toString());
        return r;
    }
    //手机端-我的-修改个人信息
    @PutMapping("/updatePatientInfo")
    public R updatePatientInfo(@RequestBody HisPatient hisPatient) {
        return hisPatientService.updatePatientInfo(hisPatient);
    }
    //手机端-我的-患者信息
    @GetMapping("/patientFile")
    public R<List<HisPatientFile>> getPatientFile() {
        return hisPatientService.getByPatientName();

    }
    //手机端-我的-患者信息-修改回显
    @GetMapping("/patientFile/{id}")
    public R<HisPatientFile> getPatientFileById(@PathVariable String id) {
        return hisPatientService.getPatientFileById(id);
    }
    //手机端-我的-患者信息-修改
    @PutMapping("/patientFile/{id}")
    public R updatePatientFile(@PathVariable String id,@RequestBody HisPatientFile hisPatientFile) {
        hisPatientFile.setPatientId(id);
        return hisPatientService.updatePatientFile(hisPatientFile);
    }
    //手机端-我的-患者信息-删除
    @DeleteMapping("/patientFile/{id}")
    public R deletePatientFile(@PathVariable String id) {
        return hisPatientService.deletePatientFile(id);
    }
    //手机端-我的-患者信息-添加
    @PostMapping("/addFile")
    public R addFile(@RequestBody HisPatientFile PatientFile) {
        return hisPatientService.addPatientFile(PatientFile);
    }
    /**
     * @create by: Zr
     * @description: 手机端-患者对医生评论-通过病例id查询医生信息
     * @create time: 2025/3/19 16:04
     * @param chId
     * @return com.khyr.vo.R<com.khyr.entity.User>
     */
    @GetMapping("/getUserByChId/{chId}")
    public R<User> getUserByChId(@PathVariable String chId) {
        return userService.getUserByChId(chId);
    }

}
